<template>
  <div class="app-container">
    <!-- 顶部导航栏 -->
    <el-header class="app-header">
      <div class="header-left">
        <div class="logo">
          <el-icon class="logo-icon"><IconMenu /></el-icon>
          <span class="logo-text">WMS管理系统</span>
        </div>
      </div>
      
      <div class="header-right">
        <div class="user-info">
          <el-avatar :size="32" class="user-avatar">
            <el-icon><Document /></el-icon>
          </el-avatar>
          <span class="username">{{ pinia.account.UserName }}</span>
          <span class="role-name">【{{ pinia.account.RoleName }}】</span>
        </div>
        <el-button 
          type="danger" 
          size="small" 
          @click="out" 
          class="logout-btn"
        >
          <el-icon><Setting /></el-icon>
          退出
        </el-button>
      </div>
    </el-header>

    <!-- 主体内容区域 -->
    <el-container class="main-container">
      <!-- 侧边菜单栏 -->
      <el-aside class="app-aside">
        <div class="menu-container">
          <el-menu 
            :default-active="$route.path" 
            router
            class="app-menu"
            background-color="#001529"
            text-color="#fff"
            active-text-color="#409eff"
            :collapse="isCollapse"
          >
            <MenuTree :menus="menuTree" />
          </el-menu>
        </div>
        
        <!-- 菜单折叠按钮 -->
        <div class="menu-toggle" @click="toggleMenu">
          <el-icon :class="{ 'is-collapsed': isCollapse }">
            <Location />
          </el-icon>
        </div>
      </el-aside>

      <!-- 主内容区域 -->
      <el-main class="app-main">
        <div class="content-wrapper">
          <RouterView />
        </div>
      </el-main>
    </el-container>
  </div>
</template>

<script lang="ts" setup>
import { Read, Write } from '@/http/interceptors'
import axios from 'axios'
import { ref } from 'vue'
import { useCounterStore } from '@/stores/counter'
const pinia = useCounterStore();
import { useRouter } from 'vue-router'
const router = useRouter();
import MenuTree from './MenuTree.vue'
import {
  Document,
  Menu as IconMenu,
  Location,
  Setting,
} from '@element-plus/icons-vue'

// 菜单树
const menuTree = ref<any[]>([]);
// 菜单折叠状态
const isCollapse = ref(false);

// 获取用户菜单树
Read.post("/api/MenuGetUserMenuTree/GetUserMenuTree", { roleIds: pinia.account.RoleId })
  .then(res => {
    menuTree.value = res.data
  });

// 切换菜单折叠状态
const toggleMenu = () => {
  isCollapse.value = !isCollapse.value;
}

// 退出
const out = () => {
  router.push('/login')
}

const handleOpen = (key: string, keyPath: string[]) => {
  console.log(key, keyPath)
}
const handleClose = (key: string, keyPath: string[]) => {
  console.log(key, keyPath)
}
</script>

<style scoped>
.app-container {
  height: 100vh;
  width: 100vw;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  position: relative;
}

/* 顶部导航栏样式 */
.app-header {
  height: 60px;
  min-height: 60px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 20px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  position: relative;
  z-index: 1000;
  flex-shrink: 0;
}

.header-left {
  display: flex;
  align-items: center;
}

.logo {
  display: flex;
  align-items: center;
  color: white;
  font-size: 18px;
  font-weight: bold;
}

.logo-icon {
  font-size: 24px;
  margin-right: 10px;
}

.logo-text {
  font-size: 20px;
  letter-spacing: 1px;
}

.header-right {
  display: flex;
  align-items: center;
  gap: 15px;
}

.user-info {
  display: flex;
  align-items: center;
  gap: 8px;
  color: white;
  font-size: 14px;
}

.user-avatar {
  background: rgba(255, 255, 255, 0.2);
  color: white;
}

.username {
  font-weight: 500;
}

.role-name {
  opacity: 0.8;
  font-size: 12px;
}

.logout-btn {
  background: rgba(255, 255, 255, 0.1);
  border: 1px solid rgba(255, 255, 255, 0.2);
  color: white;
  transition: all 0.3s ease;
}

.logout-btn:hover {
  background: rgba(255, 255, 255, 0.2);
  border-color: rgba(255, 255, 255, 0.3);
}

/* 主体容器样式 */
.main-container {
  flex: 1;
  overflow: hidden;
  height: calc(100vh - 60px);
  display: flex;
}

/* 侧边菜单栏样式 */
.app-aside {
  width: 250px;
  min-width: 250px;
  background: #001529;
  position: relative;
  transition: width 0.3s ease;
  box-shadow: 2px 0 8px rgba(0, 0, 0, 0.1);
  flex-shrink: 0;
}

.app-aside.collapsed {
  width: 64px;
  min-width: 64px;
}

.menu-container {
  height: 100%;
  overflow-y: auto;
  overflow-x: hidden;
}

.app-menu {
  border: none;
  height: 100%;
}

.app-menu :deep(.el-menu-item) {
  height: 50px;
  line-height: 50px;
  margin: 4px 8px;
  border-radius: 6px;
  transition: all 0.3s ease;
}

.app-menu :deep(.el-menu-item:hover) {
  background-color: rgba(64, 158, 255, 0.1) !important;
  color: #409eff !important;
}

.app-menu :deep(.el-menu-item.is-active) {
  background-color: #409eff !important;
  color: white !important;
  box-shadow: 0 2px 8px rgba(64, 158, 255, 0.3);
}

.app-menu :deep(.el-sub-menu__title) {
  height: 50px;
  line-height: 50px;
  margin: 4px 8px;
  border-radius: 6px;
  transition: all 0.3s ease;
}

.app-menu :deep(.el-sub-menu__title:hover) {
  background-color: rgba(64, 158, 255, 0.1) !important;
  color: #409eff !important;
}

/* 菜单折叠按钮 */
.menu-toggle {
  position: absolute;
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%);
  width: 32px;
  height: 32px;
  background: rgba(255, 255, 255, 0.1);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  color: white;
  transition: all 0.3s ease;
}

.menu-toggle:hover {
  background: rgba(255, 255, 255, 0.2);
  transform: translateX(-50%) scale(1.1);
}

.menu-toggle .is-collapsed {
  transform: rotate(180deg);
}

/* 主内容区域样式 */
.app-main {
  background: #f5f7fa;
  padding: 0;
  overflow: hidden;
  flex: 1;
  min-width: 0;
}

.content-wrapper {
  height: 100%;
  width: 100%;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 20px;
  box-sizing: border-box;
}

/* 滚动条样式 */
.menu-container::-webkit-scrollbar,
.content-wrapper::-webkit-scrollbar {
  width: 6px;
}

.menu-container::-webkit-scrollbar-track,
.content-wrapper::-webkit-scrollbar-track {
  background: transparent;
}

.menu-container::-webkit-scrollbar-thumb,
.content-wrapper::-webkit-scrollbar-thumb {
  background: rgba(255, 255, 255, 0.2);
  border-radius: 3px;
}

.menu-container::-webkit-scrollbar-thumb:hover,
.content-wrapper::-webkit-scrollbar-thumb:hover {
  background: rgba(255, 255, 255, 0.3);
}

/* 响应式设计 */
@media (max-width: 768px) {
  .app-aside {
    width: 200px;
  }
  
  .header-right {
    gap: 10px;
  }
  
  .username,
  .role-name {
    display: none;
  }
}
</style>
